export * from './YedongHtmlParser'
export * from './YedongHtmlVisitor'

// 生成目录
export interface ITocItem {
  line: number;
  tag: string;
  text: string
}

function getTitleText(node: any): string {
  return (node.props.text || '') + (node.children?.map((it: any) => getTitleText(it) || '').join('') || '')
}

export function transformToToc(ast: any[]): ITocItem[] {
  if (!ast?.length) return []

  let arr: ITocItem[] = []
  for (let i = 0; i < ast.length; i++) {
    const node = ast[i]
    const tag = node.tag
    if (tag === 'h2' || tag === 'h3' || tag === 'h4' || tag === 'h5' || tag === 'h6') {
      arr.push({tag, line: node.line, text: getTitleText(node)})
    }
  }

  return arr
}